package com.zzhrtech.wgzx_cityzen;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.loveplusplus.update.UpdateChecker;
import com.zzhrtech.wgzx_cityzen.ui.contact.ActivFragment;
import com.zzhrtech.wgzx_cityzen.ui.contact.ContactFragment;

import com.zzhrtech.wgzx_cityzen.entity.TabEntity;
import com.zzhrtech.wgzx_cityzen.ui.disease.DiseaseFragmentCopy;
import com.zzhrtech.wgzx_cityzen.ui.disease.DiseaseFragmentCopy1;
import com.zzhrtech.wgzx_cityzen.ui.me.MeFragment;
import com.zzhrtech.wgzx_cityzen.ui.task.TaskChildFragment;
import com.zzhrtech.wgzx_cityzen.ui.task.TaskFragment;
import com.zzhrtech.wgzx_cityzen.view.CustomProgress;

import java.util.ArrayList;

public class MainActivity extends BaseActivity {

    private String[] mTitles = {"问题上传", "我的上传", "活动", "更多"};
    private int[] mIconUnselectIds = {
            R.mipmap.tab_home_unselect, R.mipmap.tab_speech_unselect,
            R.mipmap.tab_jifen_unselect, R.mipmap.tab_more_unselect};
    private int[] mIconSelectIds = {
            R.mipmap.tab_home_select, R.mipmap.tab_speech_select,
            R.mipmap.tab_jifen_select, R.mipmap.tab_more_select};
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<CustomTabEntity>();
    private ArrayList<Fragment> mFragments = new ArrayList<Fragment>();

    private ViewPager vp_main;
    private CommonTabLayout tab_main;
    private MyPagerAdapter myPagerAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();

        UpdateChecker.checkForDialog(this,false);

    }

    private void initView(){

        vp_main = (ViewPager) findViewById(R.id.vp_main);
        tab_main = (CommonTabLayout) findViewById(R.id.tab_main);

        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i], mIconSelectIds[i], mIconUnselectIds[i]));
        }

        mFragments.add(DiseaseFragmentCopy1.newInstance("",""));
        mFragments.add(TaskChildFragment.newInstance());
        mFragments.add(ActivFragment.newInstance());
        mFragments.add(MeFragment.newInstance());


        myPagerAdapter = new MyPagerAdapter(getSupportFragmentManager());
        vp_main.setAdapter(myPagerAdapter);

        tab_main.setTabData(mTabEntities);

        tab_main.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                vp_main.setCurrentItem(position);

            }

            @Override
            public void onTabReselect(int position) {

            }
        });

        vp_main.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tab_main.setCurrentTab(position);

                setTitle(mTitles[position]);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        vp_main.setOffscreenPageLimit(3);
        setTitle("问题上传");
    }

    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 2 & resultCode == RESULT_OK){
            myPagerAdapter.notifyDataSetChanged();
        }
        super.onActivityResult(requestCode, resultCode, data);
    }



    @Override
    public void onBackPressed() {


        new MaterialDialog.Builder(MainActivity.this)
                .content("是否退出？")
                .negativeText("取消")
                .positiveText("确定")
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        finish();
                    }
                })
                .show();
    }
//    @Override
//    protected void onNewIntent(Intent intent) {
//        super.onNewIntent(intent);
//        setIntent(intent);// 必须要调用这句
//    }

    //
}
